<template>
  <view>
    <view>登录页面</view>
    <u-button type="primary" @click="toLogin">登录</u-button>
    <u-button type="success" @click="toOneClickLogin">去一键登录页面</u-button>
    <u-button type="warning" @click="toVerifyCodeLogin"
      >去验证码登录页面</u-button
    >
    <u-button type="primary" @click="toLogin">登录</u-button>
    <u-button type="error" @click="toLogout">退出登录</u-button>
    <u-button type="success" @click="toGetUserInfo">获取用户信息</u-button>
    <u-button type="warning" @click="back">返回</u-button>
    <view>{{ userStore.userInfo }}</view>
    <one-click-login></one-click-login>
    <verify-code-login></verify-code-login>
  </view>
</template>
<script lang="ts" setup>
import { useUserStore } from "@/store";
import { getCurrentRouteInfo } from "@/utils";
import oneClickLogin from "./oneClick.vue";
import verifyCodeLogin from "./verifyCode.vue";

const userStore = useUserStore();

const toLogin = async () => {
  const res = await userStore.login();
  console.log(14, res);
};

const toOneClickLogin = () => {
  uni.redirectTo({
    url: "/pages/login/oneClick",
  });
};

const toVerifyCodeLogin = () => {
  uni.redirectTo({
    url: "/pages/login/verifyCode",
  });
};

const toLogout = async () => {
  const res = await userStore.logout();
  console.log(19, res);
};

const toGetUserInfo = async () => {
  const res = await userStore.getInfo();
  console.log(24, res);
};

const back = () => {
  const { query } = getCurrentRouteInfo<"from" | "type">();
  const { from, type } = query;
  if (!from) {
    uni.navigateBack({
      delta: 1,
    });
  } else {
    const url = decodeURIComponent(from);
    switch (type) {
      case "switchTab":
        uni.switchTab({ url });
        break;
      case "redirectTo":
        uni.redirectTo({ url });
        break;
      default:
        uni.switchTab({ url: "/pages/index/index" });
        break;
    }
  }
};
</script>
